<?php
add_action('wp_footer', 'bt_create_google_analytics');
	function bt_create_google_analytics(){
      $google_analytics =get_option('google_analytics');
      if($google_analytics)
      {

        echo '<script type="text/javascript">';
        echo 'var _gaq = _gaq || [];';
        echo "_gaq.push(['_setAccount', '";
        echo $google_analytics ."']);";
        echo "_gaq.push(['_trackPageview']);";
        echo "(function() {";
        echo "var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;";
        echo "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';";
        echo "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);";
        echo "})();";
        echo "</script>";

      }
	}

add_action('wp_head','meta_seo_insert_head');

function meta_seo_insert_head(){
	global $post;
	global $paged;
	global $wp_query;
	$robots			= 	get_option('home_robots');
	$title 			=	get_option('home_title');
	$description 	=	get_option('home_description');
	$keywords		=	get_option('home_keywords');
	$author			= 	get_option('home_authour');
	$language		= 	get_option('language');
	
		if(!$title)			$title 		= get_bloginfo('name');
		if(!$description)	$description= get_bloginfo('description');
		if(!$keywords)		$keywords	= $description;
		if(!$author)		$author		= $title;
		if(! $language)		$language	= 'vi';	
		if(!$robots)		$robots		= 'index, follow';
	if(is_post_type_url_request('tin-tuc'))
	{
		$title='chao ban';
		$description ='abc def';
		$keywords='website dep';
	}
	if(is_single() || is_page()||is_page_template()){
		$robots 	= get_post_meta($post->ID, 'robots',true);
		$title		= get_post_meta($post->ID, 'title',true);
		$description= get_post_meta($post->ID, 'description',true);
		$keywords 	= get_post_meta($post->ID, 'keywords',true); 
		if(!$title) 		$title		 	=get_the_title();
		if(!$description) 	$description 	= bt_limit_chars(get_the_excerpt(),150);
		if(!$keywords)		$keywords		= $description;		
	}
	if ( is_tag())
	{
				$id			= get_query_var('tag_id');
				$robots		= get_tax_robots($id);				
				$title		= get_tax_title($id);
				$description= get_tax_description($id);
				$keywords	= get_tax_keywords($id);				
				if(!$title) 		$title			='Từ khóa ' . single_tag_title('',false) . ' - '. get_bloginfo('name');
				if(! $description) 	$description 	= tag_description();
		
	}
	elseif(is_category() ){
				$id			= get_query_var('cat');				
				$robots		= get_tax_robots($id);				
				$title		= get_tax_title($id);
				$description= get_tax_description($id);
				$keywords	= get_tax_keywords($id);
				
				if(!$title) 		$title			= single_term_title('',false);
				if(! $description) 	$description 	= category_description();
	}
	elseif(is_tax()){
				$term 		= get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
				$id			= $term->term_id;
				
				$robots		= get_tax_robots($id);				
				$title		= get_tax_title($id);
				$description= get_tax_description($id);
				$keywords	= get_tax_keywords($id);
				
				
				
				if(!$title) 		$title			= single_term_title('',false);
				if(! $description) 	$description 	= category_description();
	}						
	elseif (is_archive())
	{
			
		$description 	=	'';
		$keywords		=	'';
		if(is_day())
		{
			$title = get_bloginfo('name') . ' - Ngày ' . get_the_date('j/m/Y');
		}
		elseif (is_month())
		{
			$title = get_bloginfo('name') . ' - Tháng ' . get_the_date('m/Y');
		}
		elseif (is_year())
		{
			$title = get_bloginfo('name') .  ' - Năm ' . get_the_date('Y');
		}
		elseif (is_author())
		{
			$title = get_bloginfo('name') . ' - Theo người đăng';
		}
		else{
			
		}	
		 
	}
	elseif(is_404() ){
		$robots = 'noindex,follow';
	}

	if ($paged>1)
	{
		$robots		 = 'noindex,nofollow';
		$title 		.= ' - Trang ' . $paged; 
		$description ='';
		$keywords	 = '';
	}
	?>
	 <meta name="author" content="<?php echo $author; ?>" />
     <meta http-equiv="content-language" content="<?php echo $language; ?>">
     <meta name="robots" content="<?php echo $robots; ?>">
     <title><?php echo $title; ?></title>
     <meta name="description" content="<?php echo $description; ?>" />
     <meta name="keywords" content="<?php echo $keywords; ?>" />
	<?php 
}
function limit_meta_keywords($text, $ilen =60){
		if(strlen($text)>$ilen){
			$text = substr($text, 0,strpos($text, ' ',$ilen));
		}
		return $text;
}

function get_tax_robots($id)
{
	$meta = get_option('seo_box');
	if (empty($meta)) $meta = array();
	if (!is_array($meta)) $meta = (array) $meta;
	$meta = isset($meta[$id]) ? $meta[$id] : array();
	$value = $meta['seo_robots'];
	return $value;
}
function get_tax_title($id)
{
	$meta = get_option('seo_box');
	if (empty($meta)) $meta = array();
	if (!is_array($meta)) $meta = (array) $meta;
	$meta = isset($meta[$id]) ? $meta[$id] : array();
	$value = $meta['seo_title'];
	return $value;
}
function get_tax_description($id)
{
	$meta = get_option('seo_box');
	if (empty($meta)) $meta = array();
	if (!is_array($meta)) $meta = (array) $meta;
	$meta = isset($meta[$id]) ? $meta[$id] : array();
	$value = $meta['seo_description'];
	return $value;
}
function get_tax_keywords($id)
{
	$meta = get_option('seo_box');
	if (empty($meta)) $meta = array();
	if (!is_array($meta)) $meta = (array) $meta;
	$meta = isset($meta[$id]) ? $meta[$id] : array();
	$value = $meta['seo_keywords'];
	return $value;
}